package com.alliance.shuwei.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alliance.shuwei.entity.User;
import com.alliance.shuwei.repository.UserDao;

@Service
@Transactional
public class UserService {
	private final UserDao userDao;

	@Autowired
	public UserService(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public List<User> getAllUsers() {
		List<User> users = new ArrayList<User>();
		Iterator<User> iterator = userDao.findAll().iterator();
		while (iterator.hasNext()) {
			users.add(iterator.next());
		}
		return users;
	}
	
	public User createUser(User user){
		return userDao.save(user);
	}
	public User updateUser(User user){
		return userDao.save(user);
	}
	
	public User getUserByName(String name) {
		return userDao.findByName(name);
	}
	public User getUserById(int id) {
		return userDao.findOne(id);
	}
}
